package com.example.androidframework.ui.course.coroutine

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.androidframework.R
import com.martin.lib_base.ktx.logw
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class CoroutineActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_coroutine)

        GlobalScope.launch(Dispatchers.IO) {
            // 挂起
            delay(10000)
            // 多次连续点击后,10秒以后会多次打印
            logw("currentThread === ${Thread.currentThread().name}")
        }

        MainScope().launch {

        }

        // 阻塞
        Thread.sleep(10000)
        // 多次连续点击后,10秒以后只会一次打印,会阻塞主线程,导致丢帧,可能闪退
        logw("currentThread === ${Thread.currentThread().name}")
    }

    companion object {
        fun start(context: Context) {
            val intent = Intent(context, CoroutineActivity::class.java)
            context.startActivity(intent)
        }
    }
}